package com.xbongbong.saas.script.tempenum;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.SerialNumberPoJo;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.ForbiddenSettingEnum;
import com.xbongbong.saas.enums.InstockTypeEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.InstockEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.sys.enums.RoleEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 入库单
 *
 * @author Jian.Wang
 * @version v1.0
 * @date 2019/1/19
 * @since v1.0
 */
@Getter
public enum TempInstockEnum {
    //
    BASE_INFO(InstockEnum.BASE_INFO),
    SHEET_NO(InstockEnum.SHEET_NO),
    TIME(InstockEnum.TIME),
    TYPE(InstockEnum.TYPE),
    REF_ID(InstockEnum.REF_ID),
    WAREHOUSE_ID(InstockEnum.WAREHOUSE_ID),
    PRODUCT_INFO(InstockEnum.PRODUCT_INFO),
    PRODUCT(InstockEnum.PRODUCT),
    EXTEND_INFO(InstockEnum.EXTEND_INFO),
    MANAGER_ID(InstockEnum.MANAGER_ID),
    MEMO(InstockEnum.MEMO),

    SYSTEM_INFO(InstockEnum.SYSTEM_INFO),
    CREATOR_ID(InstockEnum.CREATOR_ID),
    ADD_TIME(InstockEnum.ADD_TIME),
    UPDATE_TIME(InstockEnum.UPDATE_TIME),


    INCOMING(InstockEnum.INCOMING),
    WAREHOUSE_ID_LINKED_TEXT(InstockEnum.WAREHOUSE_ID_LINKED_TEXT),
    REF_ID_LINKED_TEXT(InstockEnum.REF_ID_LINKED_TEXT),

    ;
    private InstockEnum anEnum;

    TempInstockEnum(InstockEnum instockEnum) {
        this.anEnum = instockEnum;
    }
    public static List<TempInstockEnum> getAllEnum() {
        java.util.List<TempInstockEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }
    /**
     * 初始化编号
     *
     */
    public static void initSheetNo(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.INSTOCK.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.INSTOCK.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(SHEET_NO.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        // 不能重复
        fieldAttrEntity.setNoRepeat(1);
        // 默认的编号规则
        SerialNumberPoJo serialNumberPoJo = InitExplainsHelper.setSerialNumber(SerialNumberEnum.QT_INSTOCK_NO);
        fieldAttrEntity.setSerialNumber(serialNumberPoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化日期
     *
     * @param fieldAttrEntity
     */
    public static void initTime(FieldAttrEntity fieldAttrEntity) {
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType("today");
        fieldAttrEntity.setDateType(DateTimeEnum.TIME.getType());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setIsForbidden(0);
    }
    /**
     * 初始化关联客户
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initWarehouseId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.WAREHOUSE.getCode());
        fieldAttrEntity.setLinkedText(WAREHOUSE_ID_LINKED_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("base");
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(1);
    }

    /**
     * 初始化入库类型
     *
     * @author chaoxiong.lei
     * @date  11:01 2019/1/18
     **/
    public static void initType(FieldAttrEntity fieldAttrEntity) {
        // 将出库类型设置为下拉选项
        //Map<Integer, String> instockTypeMap = InstockTypeEnum.getOtherInstockTypeMap();
        List<ItemPoJo> itemPoJos = InitExplainsHelper.setCombItemByEnum(InstockTypeEnum.class, fieldAttrEntity.getFieldType());
        List<String> otherInstockCodeList = Arrays.asList(InstockTypeEnum.INVENTORY_INSTOCK.getName(),
                InstockTypeEnum.TRANSFER_INSTOCK.getName(), InstockTypeEnum.OTHER_INSTOCK.getName(),
                InstockTypeEnum.ASSEMBLE_INSTOCK.getName());
        for (ItemPoJo itemPoJo: itemPoJos) {
            itemPoJo.setNoDel(1);
            itemPoJo.setNoEdit(1);
            if (!otherInstockCodeList.contains(itemPoJo.getText())) {
                itemPoJo.setShowType(ShowTypeEnum.NONE.getCode());
                itemPoJo.setEditHide(1);
            } else {
                if (Objects.equals(itemPoJo.getText(), InstockTypeEnum.OTHER_INSTOCK.getName())) {
                    itemPoJo.setChecked(true);
                } else {
                    itemPoJo.setFieldMap(Arrays.asList(InstockEnum.REF_ID.getAttr()));
                    itemPoJo.setShowType(ShowTypeEnum.EXCLUDE_NEW.getCode());
                }
            }
        }
        // 不允许设置其他的选项为默认值
        fieldAttrEntity.setItemBanDefault(1);
        fieldAttrEntity.setItems(itemPoJos);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setShowEditable(0);
    }

    public static void initRefId(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setLinkedText(REF_ID_LINKED_TEXT.anEnum.getAttr());
        fieldAttrEntity.setIsMultiBusiness(1);
        fieldAttrEntity.setBusinessType(XbbRefTypeEnum.INSTOCK.getCode());
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);

        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_VISIBLE_CORRELATION_FIELD.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    /**
     * 物流公司
     */
    public static void initExpressNo(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setComboType(ComboTypeEnum.CUSTOM.getType());
        fieldAttrEntity.setDictionaryFlag(0);
    }

    public static void initCreatorId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initAddTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initUpdateTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initManagerId(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setMultiple(0);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setRequired(1);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.WAREHOUSE_MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.WAREHOUSE_MANAGER));
    }

    public static void initProducts(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setIsForbidden(0);
    }
}
